﻿using QLDVu.BO;
using QLKetNoi;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Linq;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QLDVu.GUI
{
    public partial class BaoCaoNo : Form
    {
        public BaoCaoNo()
        {
            InitializeComponent();
        }
        public static int thang;
        public static int nam;
        public static string NextID(string lastID, string prefixID)
        {
            int nextID = int.Parse(lastID.Remove(0, prefixID.Length)) + 1;
            int lengthNumerID = lastID.Length - prefixID.Length;
            string zeroNumber = "";
            for (int i = 1; i <= lengthNumerID; i++)
            {
                if (nextID < Math.Pow(10, i))
                {
                    for (int j = 1; j <= lengthNumerID - i; i++)
                    {
                        zeroNumber += "0";
                    }
                    return prefixID + zeroNumber + nextID.ToString();
                }
            }
            return prefixID + nextID;
        }
        private void BaoCaoNo_Load(object sender, EventArgs e)
        {
            dpThangBaoCao.CustomFormat = "MM/yyyy";
            dpThangBaoCao.Format = DateTimePickerFormat.Custom;
        }

        private void btnBaoCao_Click(object sender, EventArgs e)
        {


            int _thang = dpThangBaoCao.Value.Month;
            int _nam = dpThangBaoCao.Value.Year;

            QLDNDataContext dulieu = new QLDNDataContext();
            Table<SUACHUA> suachua = dulieu.GetTable<SUACHUA>();
            Table<BAOGIA> baogia = dulieu.GetTable<BAOGIA>();
            Table<PHIEUTHU> phieuthutien = dulieu.GetTable<PHIEUTHU>();
            Table<KHACHHANG> khachhang = dulieu.GetTable<KHACHHANG>();
            Table<BAOCAONOTIEN> baocaono = dulieu.GetTable<BAOCAONOTIEN>();
            Table<CHITIETBAOCAONO> chitietbaocaono = dulieu.GetTable<CHITIETBAOCAONO>();

               var dsbaocaono = from bcno in baocaono
                           from ctbc in chitietbaocaono
                           from kh in khachhang
                             where bcno.MaBaoCao == ctbc.MaBaoCao && bcno.ThangBaoCao.Month.Equals(_thang) && bcno.ThangBaoCao.Year.Equals(_nam) && kh.MaKhachHang==ctbc.MaKhachHang
                     select new
                     {
                         ctbc.MaChiTiet,ctbc.MaBaoCao,ctbc.MaKhachHang,kh.TenKhachHang,ctbc.NoDau,ctbc.NoPhatSinh,ctbc.NoCuoi
                     };
               string mabaocao = "";
            if (dsbaocaono.ToList().Count==0)
            {
                string mabaocaono = "BCN0001";
                if (BaoCaoNoBO.BaoCaoNoCuoiCung() != null)
                    mabaocaono = NextID(BaoCaoNoBO.BaoCaoNoCuoiCung().MaBaoCao, "BCN");
                BaoCaoNoBO.ThemBaoNo(mabaocaono, dpThangBaoCao.Value.ToShortDateString());
                foreach (KHACHHANG kh in khachhang)
                {
                    var ds = from dsuachua in suachua
                             from dbaogia in baogia
                             where dsuachua.MaBaoGia == dbaogia.MaBaoGia && dsuachua.NgayVao.Month.Equals(_thang) && dsuachua.NgayVao.Year.Equals(_nam) && dbaogia.MaKhachHang.Equals(kh.MaKhachHang)
                             select new
                             {
                                 TongTien = dbaogia.TongTien,
                                 MaPhieuSuaChua = dsuachua.MaSuaChua
                             };
                    int tienno = 0;
                    int tiendatra = 0;
                    string masuachua = "";
                    int tienphatsinh = 0;
                    if (ds.ToList().Count > 0)
                    {
                        for (int i = 0; i < ds.ToList().Count; i++)
                        {
                            tienno = tienno + Convert.ToInt32(ds.ToList()[i].TongTien);
                            masuachua = ds.ToList()[i].MaPhieuSuaChua.ToString();
                            tiendatra = 0;
                            var pt = from dsphieuthu in phieuthutien

                                     where dsphieuthu.NgayThuTien.Month.Equals(_thang) && dsphieuthu.NgayThuTien.Year.Equals(_nam) && dsphieuthu.MaPhieuSuaChua.Equals(masuachua)
                                     select new
                                     {
                                         dsphieuthu.MaPhieuThuTien,
                                         dsphieuthu.TienThu,
                                         dsphieuthu.TienConLai
                                     };
                            if (pt.ToList().Count > 0)
                            {
                                for (int j = 0; j < pt.ToList().Count; j++)
                                    tiendatra = tiendatra + Convert.ToInt32(pt.ToList()[j].TienThu);
                            }


                        }

                    }
                    tienphatsinh = tienno - tiendatra;
                    int nodau = 0;
                    var q = from bc in baocaono
                            from ctbc in chitietbaocaono
                            where ctbc.MaBaoCao == bc.MaBaoCao && bc.ThangBaoCao.Month < _thang && bc.ThangBaoCao.Year <= _nam && ctbc.MaKhachHang.Equals(kh.MaKhachHang)
                            select new
                            {
                                ctbc.NoCuoi,
                                bc.ThangBaoCao
                            };

                    if (q.ToList().Count > 0)
                    {
                        DateTime maxdate = Convert.ToDateTime(q.ToList()[0].ThangBaoCao);
                        for (int i = 0; i < q.ToList().Count - 1; i++)
                        {

                            for (int j = i + 1; j < q.ToList().Count; j++)
                            {
                                DateTime date2 = Convert.ToDateTime(q.ToList()[j].ThangBaoCao);
                                if (maxdate < date2)
                                    maxdate = date2;
                            }

                        }
                        for (int i = 0; i < q.ToList().Count; i++)
                        {
                            if (Convert.ToDateTime(q.ToList()[i].ThangBaoCao) == maxdate)
                            {
                                nodau = Convert.ToInt32(q.ToList()[i].NoCuoi);
                            }
                        }
                    }


                    string machitiet = "CT0001";
                    if (BaoCaoNoBO.ChiTietBaoCaoNoCuoiCung() != null)
                        machitiet = NextID(BaoCaoNoBO.ChiTietBaoCaoNoCuoiCung().MaChiTiet, "CT");

                    int nocuoi = nodau + tienphatsinh;
                    BaoCaoNoBO.ThemChiTietBaoNo(machitiet, mabaocaono, kh.MaKhachHang, nodau.ToString(), nocuoi.ToString(), tienphatsinh.ToString());

                }
            }
            else if (dsbaocaono.ToList().Count > 0)
            {
                mabaocao = dsbaocaono.ToList()[0].MaBaoCao;
                BaoCaoNoBO.XoaChiTietBaoCao(mabaocao);
                foreach (KHACHHANG kh in khachhang)
                {
                    var ds = from dsuachua in suachua
                             from dbaogia in baogia
                             where dsuachua.MaBaoGia == dbaogia.MaBaoGia && dsuachua.NgayVao.Month.Equals(_thang) && dsuachua.NgayVao.Year.Equals(_nam) && dbaogia.MaKhachHang.Equals(kh.MaKhachHang)
                             select new
                             {
                                 TongTien = dbaogia.TongTien,
                                 MaPhieuSuaChua = dsuachua.MaSuaChua
                             };
                    int tienno = 0;
                    int tiendatra = 0;
                    string masuachua = "";
                    int tienphatsinh = 0;
                    if (ds.ToList().Count > 0)
                    {
                        for (int i = 0; i < ds.ToList().Count; i++)
                        {
                            tienno = tienno + Convert.ToInt32(ds.ToList()[i].TongTien);
                            masuachua = ds.ToList()[i].MaPhieuSuaChua.ToString();
                            tiendatra = 0;
                            var pt = from dsphieuthu in phieuthutien

                                     where dsphieuthu.NgayThuTien.Month.Equals(_thang) && dsphieuthu.NgayThuTien.Year.Equals(_nam) && dsphieuthu.MaPhieuSuaChua.Equals(masuachua)
                                     select new
                                     {
                                         dsphieuthu.MaPhieuThuTien,
                                         dsphieuthu.TienThu,
                                         dsphieuthu.TienConLai
                                     };
                            if (pt.ToList().Count > 0)
                            {
                                for (int j = 0; j < pt.ToList().Count; j++)
                                    tiendatra = tiendatra + Convert.ToInt32(pt.ToList()[j].TienThu);
                            }


                        }

                    }
                    tienphatsinh = tienno - tiendatra;
                    int nodau = 0;
                    var q = from bc in baocaono
                            from ctbc in chitietbaocaono
                            where ctbc.MaBaoCao == bc.MaBaoCao && bc.ThangBaoCao.Month < _thang && bc.ThangBaoCao.Year <= _nam && ctbc.MaKhachHang.Equals(kh.MaKhachHang)
                            select new
                            {
                                ctbc.NoCuoi,
                                bc.ThangBaoCao
                            };

                    if (q.ToList().Count > 0)
                    {
                        DateTime maxdate = Convert.ToDateTime(q.ToList()[0].ThangBaoCao);
                        for (int i = 0; i < q.ToList().Count - 1; i++)
                        {

                            for (int j = i + 1; j < q.ToList().Count; j++)
                            {
                                DateTime date2 = Convert.ToDateTime(q.ToList()[j].ThangBaoCao);
                                if (maxdate < date2)
                                    maxdate = date2;
                            }

                        }
                        for (int i = 0; i < q.ToList().Count; i++)
                        {
                            if (Convert.ToDateTime(q.ToList()[i].ThangBaoCao) == maxdate)
                            {
                                nodau = Convert.ToInt32(q.ToList()[i].NoCuoi);
                            }
                        }
                    }

                    string machitiet = "CT0001";
                    if (BaoCaoNoBO.ChiTietBaoCaoNoCuoiCung() != null)
                        machitiet = NextID(BaoCaoNoBO.ChiTietBaoCaoNoCuoiCung().MaChiTiet, "CT");
                    int nocuoi = nodau + tienphatsinh;
                    BaoCaoNoBO.ThemChiTietBaoNo(machitiet, mabaocao, kh.MaKhachHang, nodau.ToString(), nocuoi.ToString(), tienphatsinh.ToString());
                }     
            }

            var tongtien = from bcno in baocaono
                             from ctbc in chitietbaocaono
                             where bcno.MaBaoCao == ctbc.MaBaoCao && bcno.ThangBaoCao.Month.Equals(_thang) && bcno.ThangBaoCao.Year.Equals(_nam)
                           group ctbc by new { ctbc.MaBaoCao } into g
                             select new
                             {
                                 TongTienNo=g.Sum(s=>s.NoCuoi)
                             };
            gvBaoCaoNo.DataSource = dsbaocaono;
            txtTienNo.Text = tongtien.ToList()[0].TongTienNo.ToString();

        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            thang = dpThangBaoCao.Value.Month;
            nam = dpThangBaoCao.Value.Year;
            InBaoCaoNo inform=new InBaoCaoNo();
            inform.Show();
        }
       

    }
}
